500B - New Year Permutation - CodeForces Solution


dfs and similar dsu graphs greedy math sortings *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define flush fflush(stdout)
#define ULL unsigned long long
#define LL long long
#define LD long double
#define EPS 0.000000001
#define MOD 1000000007
#define endl '\n'
#define ALL(x) x.begin(),x.end()
//#pragma GCC optimize("trapv")
#pragma GCC optimize("-Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")


using namespace std;

short dx[] = { 1,-1,0,0 };
short dy[] = { 0,0,1,-1 };

LL FPow(int a, int b)
{
	if (b == 0)
		return 1;
	int res = FPow(a, b / 2);
	if (b % 2)
		return (res * res * a);
	else
		return (res * res);
}
vector<vector<int>> graph(305);
bool vis[305];
int32_t main() {
	//freopen("reduce.in", "r", stdin);
	//freopen("reduce.out", "w", stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	vector<int> permutation(n+1);
	for (int i = 1; i <= n; i++)
	{
		cin >> permutation[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			char a;
			cin >> a;
			if (a == '1')
			{
				graph[i].push_back(j);
			}
		}
	}
	vector<pair<int, int>> ans;
	for (int i = 1; i <= n; i++)
	{
		if (!vis[i])
		{
			queue<int> q;
			vector<int> idxs;
			q.push(i);
			idxs.push_back(i);
			vis[i] = true;
			while (q.size())
			{
				int cur = q.front();
				for (int child = 0; child < graph[cur].size(); child++)
				{
					if (!vis[graph[cur][child]])
					{
						idxs.push_back(graph[cur][child]);
						q.push(graph[cur][child]);
						vis[graph[cur][child]] = true;
					}
				}
				q.pop();
			}
			sort(ALL(idxs));
			vector<int> temp;
			temp.push_back(-1);
			for (int idx = 0; idx < idxs.size(); idx++)
			{
				temp.push_back(permutation[idxs[idx]]);
			}
			sort(ALL(temp));
			int cnt = 1;
			for (int k = 0; k < idxs.size(); k++)
			{
				permutation[idxs[k]] = temp[cnt];
				cnt++;
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		cout << permutation[i] << ' ';
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters